package com.haoshuang.zhouzhoubang.ui.vm

import com.haoshuang.zhouzhoubang.model.api.ApiRepo
import com.haoshuang.zhouzhoubang.model.bean.CallBean
import com.haoshuang.zhouzhoubang.model.bean.EwBean
import com.haoshuang.zhouzhoubang.model.bean.FocusDateBean
import com.haoshuang.zhouzhoubang.model.bean.OrderBean
import com.haoshuang.zhouzhoubang.model.bean.OrderDetail2Bean
import com.haoshuang.zhouzhoubang.model.bean.SmBean
import com.haoshuang.zhouzhoubang.model.bean.YanShou
import com.mufeng.libs.base.BaseViewModel
import com.mufeng.libs.net.netRequest
import com.mufeng.libs.state.StateData
import com.mufeng.libs.state.StatefulMutableLiveData


class OrderListStatusViewModel : BaseViewModel() {

    /**
     * 确认上门
     */
    val confirmDoorData = StatefulMutableLiveData<Any>()
    fun confirmDoor(orderId: String) = netRequest {
        start { confirmDoorData.value = StateData.Loading }
        request { ApiRepo.confirmDoor(orderId)  }
        success { confirmDoorData.value = StateData.Success(it.data) }
        error { code, msg ->
            confirmDoorData.value = StateData.Error(code, msg)
        }
    }

    /**
     * 是否可以确认上门
     */
    val isConfirmDoorVisitData = StatefulMutableLiveData<SmBean>()
    fun isConfirmDoorVisit(orderId: String, lat: String, lng: String) = netRequest {
        start { isConfirmDoorVisitData.value = StateData.Loading }
        request { ApiRepo.isConfirmDoorVisit(orderId, lat, lng) }
        success { isConfirmDoorVisitData.value = StateData.Success(it.data) }
        error { code, msg ->
            isConfirmDoorVisitData.value = StateData.Error(code, msg)
        }
    }


    /**
     * 订单详情
     */
    val getMasterOrderDetailData = StatefulMutableLiveData<OrderDetail2Bean>()
    fun getMasterOrderDetail(orderId: String) = netRequest {
        start { getMasterOrderDetailData.value = StateData.Loading }
        request { ApiRepo.getMasterOrderDetail(orderId) }
        success { getMasterOrderDetailData.value = StateData.Success(it.data) }
        error { code, msg ->
            getMasterOrderDetailData.value = StateData.Error(code, msg)
        }
    }

    /**
     * 获取额外费用记录
     */
    val getOtherCostData = StatefulMutableLiveData<EwBean>()
    fun getOtherCost(orderId: String) = netRequest {
        start { getOtherCostData.value = StateData.Loading }
        request { ApiRepo.getOtherCost(orderId) }
        success { getOtherCostData.value = StateData.Success(it.data) }
        error { code, msg ->
            getOtherCostData.value = StateData.Error(code, msg)
        }
    }

    /**
     * 提醒用户
     */
    val remindCheckData = StatefulMutableLiveData<String>()
    fun remindCheck(orderId: String) = netRequest {
        start { remindCheckData.value = StateData.Loading }
        request { ApiRepo.remindCheck(orderId) }
        success { remindCheckData.value = StateData.Success(orderId) }
        error { code, msg ->
            remindCheckData.value = StateData.Error(code, msg)
        }
    }

    /**
     * 聚焦日历数据
     */
    val getMasterCalendarData = StatefulMutableLiveData<MutableList<FocusDateBean>>()
    fun getMasterCalendar(status: Int, date: String) = netRequest {
        start { getMasterCalendarData.value = StateData.Loading }
        request { ApiRepo.getMasterCalendar(status, date) }
        success { getMasterCalendarData.value = StateData.Success(it.data) }
        error { code, msg ->
            getMasterCalendarData.value = StateData.Error(code, msg)
        }
    }

    /**
     * 获取订单列表
     */
    val getMasterOrderListData = StatefulMutableLiveData<MutableList<OrderBean>>()
    fun getMasterOrderList(
        status: Int,
        page: Int,
        date: String = "",
        date_type: Int = 0,
        keyword: String = "",
        is_rework: Int = 0,
        start_date: String = "",
        end_date: String = "",
        hexiao_type: String = "",
    ) = netRequest {
        start { getMasterOrderListData.value = StateData.Loading }
        request {
            ApiRepo.getMasterOrderList(
                status,
                page,
                date,
                date_type,
                keyword,
                is_rework,
                start_date,
                end_date,hexiao_type
            )
        }
        success { getMasterOrderListData.value = StateData.Success(it.data) }
        error { code, msg ->
            getMasterOrderListData.value = StateData.Error(code, msg)
        }
    }
    
    /**
     * 修改确认上门时间
     */
    val confirmUpdateServiceTimeData = StatefulMutableLiveData<String>()
    fun confirmUpdateServiceTime(orderId: String, service_day: String, service_time: String) = netRequest {
        start { confirmUpdateServiceTimeData.value = StateData.Loading }
        request { ApiRepo.confirmUpdateServiceTime(orderId, service_day, service_time) }
        success { confirmUpdateServiceTimeData.value = StateData.Success(it.data) }
        error { code, msg ->
            confirmUpdateServiceTimeData.value = StateData.Error(code, msg)
        }
    }

    /**
     * 出示验收码
     */
    val checkOrderData = StatefulMutableLiveData<YanShou>()
    fun checkOrder(orderId: String) = netRequest {
        start { checkOrderData.value = StateData.Loading }
        request { ApiRepo.checkOrder(orderId) }
        success {
            val yanShou = it.data as YanShou?
            yanShou?.orderId = orderId
            checkOrderData.value = StateData.Success(it.data)
        }
        error { code, msg ->
            checkOrderData.value = StateData.Error(code, msg)
        }
    }

    /**
     * 上传服务中图片视频
     */
    val doServiceData = StatefulMutableLiveData<Any>()
    fun doService(orderId: String, service_img: String = "", service_video: String = "") = netRequest {
        start { doServiceData.value = StateData.Loading }
        request { ApiRepo.doService(orderId, service_img, service_video) }
        success { doServiceData.value = StateData.Success(it.data) }
        error { code, msg ->
            doServiceData.value = StateData.Error(code, msg)
        }
    }

}